package com.jbh.fourclibrary.activity;

import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.VideoView;

import com.jbh.fourclibrary.R;


public class PlayMp4Activity extends AppCompatActivity {
    private VideoView mVideoView;
    private ImageView imBack;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); //隐藏状态栏
//        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); //显示状态栏
        setContentView(R.layout.activity_play_mp4);
        initView();
    }

    /**
     */
    private void initView() {
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
        String  av_name = getIntent().getStringExtra("av_name");
        mVideoView=findViewById(R.id.mVideoView);
        mVideoView.setVideoURI(Uri.parse("android.resource://"+getPackageName()+"/raw/"+av_name));
        final MediaController mediaController=new MediaController(this);
        mediaController.setPadding(20,0,20,0);
        mediaController.show();
        mVideoView.setMediaController(mediaController);
        mVideoView.requestFocus();
        mVideoView.start();
        imBack=findViewById(R.id.imBack);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                imBack.setVisibility(View.GONE);
            }
        },1000);
        imBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mVideoView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        if (!isVisiably){
                            isVisiably = true ;
                            imBack.setVisibility(View.VISIBLE);
                            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); //显示状态栏
                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    isVisiably = false ;
                                    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); //隐藏状态栏
                                    imBack.setVisibility(View.GONE);
                                }
                            },2000);
                        }
                        break;
                }
                return false;
            }
        });
    }

    boolean isVisiably ;

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mVideoView.stopPlayback();
        mVideoView = null;
    }
}
